package com.hongdee.base.imp.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.base.imp.entity.TDataEntity;
import com.hongdee.base.imp.entity.TDataEntityProperty;
import com.hongdee.base.imp.service.IDataEntityPropertyService;
import com.hongdee.base.imp.service.IDataEntityService;
import com.hongdee.common.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 名  称:DataEntityController
 * 类  型: class
 * 描  述: 数据实体控制类
 * 路  径:com.hongdee.base.imp.controller
 * 作  者:hah
 * 日  期:2017/2/14 08:21
 */
@RestController
@RequestMapping("/base/dataentity")
public class DataEntityController {

    @Autowired
    private IDataEntityService dataEntityService;

    @Autowired
    private IDataEntityPropertyService dataEntityPropertyService;


    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "qualifiedName", defaultValue = "") String qualifiedName) throws Exception {
        Map map = dataEntityService.page(start, limit, qualifiedName);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TDataEntity data) throws Exception {
        EntityUtils.valid(data);
        dataEntityService.saveOrUpdate(data,uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    /**
     * 初始化属性列表
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/init")
    @ResponseBody
    public ResultEntity init(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择记录！");
        }
        /***
         * 获取实体名称
         */
        Map entity = dataEntityService.info(id);
        if (null == entity || 0 == entity.size()) {
            throw new Exception("不存在对应的数据!,请重新选择！");
        }

        List<TDataEntityProperty> proList = dataEntityPropertyService.initPropertyList((String) entity.get("id"),
                (String) entity.get("qualifiedName"));

        if (null != proList && proList.size() > 0) {
            //先删除，后增加
            dataEntityPropertyService.clearById(id);
            dataEntityPropertyService.saveOrUpdate(proList);
        }

        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("保存成功！");

        return res;
    }

    /**
     * 启用
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        dataEntityService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }

    /**
     * 停用
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        dataEntityService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }

    /**
     * 查询详情
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择记录");
        }
        Map map = dataEntityService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * 根据 EntityID，查询可导入的属性
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/props")
    @ResponseBody
    public Map props(@RequestParam(name = "uid", defaultValue = "") String uid,
                     @RequestParam(name = "token", defaultValue = "") String token,
                     @RequestParam(name = "start", defaultValue = "0") String start,
                     @RequestParam(name = "limit", defaultValue = "50") String limit,
                     @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择记录！");
        }
        Map res = dataEntityPropertyService.page(start, limit, id);
        res.put("success", true);
        res.put("msg", "分页查询成功！");
        return res;
    }

    /**
     * 保存属性实体
     *
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveProps")
    @ResponseBody
    public ResultEntity saveProps(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestBody TDataEntityProperty data) throws Exception {
        EntityUtils.valid(data);
        dataEntityPropertyService.saveOrUpdateProp(data);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    @RequestMapping("/combo")
    @ResponseBody
    public List queryCombo(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        return this.dataEntityService.combo();

    }


}
